package io.renren.modules.media.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import io.renren.modules.media.entity.MediaInfoEntity;
import io.renren.modules.media.ffmpeg.GetVideoInfo;
import io.renren.modules.media.model.VideoInfo;
import io.renren.modules.media.service.MediaInfoService;
import io.renren.modules.media.support.MinioSupport;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author zctel
 * @email 845990668@qq.com
 * @date 2021-10-16 15:59:18
 */
@RestController
@RequestMapping("meida/mediainfo")
public class MediaInfoController {
    @Autowired
    private MediaInfoService mediaInfoService;
    
    @Autowired
    private MinioSupport minioSupport;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("meida:mediainfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = mediaInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("meida:mediainfo:info")
    public R info(@PathVariable("id") Integer id){
		MediaInfoEntity mediaInfo = mediaInfoService.getById(id);

        return R.ok().put("mediaInfo", mediaInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("meida:mediainfo:save")
    public R save(@RequestBody MediaInfoEntity mediaInfo){
		mediaInfoService.save(mediaInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("meida:mediainfo:update")
    public R update(@RequestBody MediaInfoEntity mediaInfo){
		mediaInfoService.updateById(mediaInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("meida:mediainfo:delete")
    public R delete(@RequestBody Integer[] ids){
		mediaInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    
	/**
	 * 上传文件
	 */
	@PostMapping("/upload")
	//@RequiresPermissions("meida:mediainfo:all")
	public R upload(@RequestParam("file") MultipartFile file) throws Exception {
		if (file.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}
		String alias = file.getOriginalFilename();
		if(!(alias.endsWith(".mp4") || alias.endsWith(".mp3"))) {
			throw new RRException("文件格式错误");
		}

		//上传文件
		String path = "video/"+alias;
		minioSupport.uploadFile(file.getInputStream(), "media", path, null);
		//保存文件信息
		mediaInfoService.saveInfo(alias, "video/", file.getInputStream());
		return R.ok();
	}

}
